/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.saber.certificacao.beansjsf.cadastro;

import br.com.saber.certificacao.beansjsf.interfacebean.BeanJSFInterface;
import br.com.saber.certificacao.beansjsf.login.LoginManager;
import br.com.saber.certificacao.beansjsf.semescopo.SelectItemManager;
import br.com.saber.certificacao.ejbs.stateless.interfaces.CertificacaoBeanLocal;
import br.com.saber.certificacao.modelos.Certificacao;
import br.com.saber.certificacao.modelos.validadores.exceptions.generica.ValidacaoExcecao;
import br.com.saber.certificacao.observador.ControleObserver;
import br.com.saber.certificacao.utils.UtilMetodos;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author gbvbahia
 */
@ManagedBean(name = "cadastroCertificacao")
@ViewScoped
public class CertificacaoManager implements BeanJSFInterface {

    @EJB
    private CertificacaoBeanLocal certificacaoBean;

    @ManagedProperty("#{loginManager}")
    private LoginManager loginManager;

    @ManagedProperty("#{selectItemManager}")
    private SelectItemManager selectItemManager;

    private Certificacao certificacao;

    private List<Certificacao> modelCertificacoes;

    /** Creates a new instance of CertificacaoManager */
    public CertificacaoManager() {
    }

    //====================
    // Iniciadores
    //====================
    @Override
    @PostConstruct
    public void init() {
        this.certificacao = new Certificacao();
        this.certificacao.setCoordenador(loginManager.getUsuario());
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "CertificacaoManager.init() executado!");
    }

    @Override
    @PreDestroy
    public void end() {
        Logger.getLogger(this.getClass().getName()).log(Level.FINEST, "CertificacaoManager.end() executado!");
    }
    //====================
    //Métodos de Negócio
    //====================

    public void salvarCertificacao(ActionEvent event) {
        try {
            certificacaoBean.salvarCertificacao(certificacao);
            init();
            ControleObserver.notificaObservers(loginManager.getUsuario(), new String[]{ControleObserver.Eventos.CAD_CERTIFICACAO_EVENT});
            UtilMetodos.messageFactoringFull("certificacaoCadastradaOK", FacesMessage.SEVERITY_INFO, FacesContext.getCurrentInstance());
        } catch (ValidacaoExcecao e) {
            if (!StringUtils.isBlank(e.getAtributoName())) {
                UtilMetodos.messageFactoringFull(UtilMetodos.getResourceBundle(e.getMessage(), FacesContext.getCurrentInstance()), null, e.getAtributoName(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            } else {
                UtilMetodos.messageFactoringFull(e.getMessage(), FacesMessage.SEVERITY_ERROR, FacesContext.getCurrentInstance());
            }
        }
        atualizarModelCertificacoes();
    }

    public void atualizarModelCertificacoes() {
        this.modelCertificacoes = new ArrayList(certificacaoBean.buscarTodasCertificacoes());
    }

    //====================
    //Table Actions
    //====================

    //====================
    //SelectItem
    //====================
    public List<SelectItem> getCoordenadores() {
        return this.selectItemManager.getCoordenadores();
    }
    //=========================
    //Getters AND Setters
    //=========================

    public LoginManager getLoginManager() {
        return loginManager;
    }

    public void setLoginManager(LoginManager loginManager) {
        this.loginManager = loginManager;
    }

    public SelectItemManager getSelectItemManager() {
        return selectItemManager;
    }

    public void setSelectItemManager(SelectItemManager selectItemManager) {
        this.selectItemManager = selectItemManager;
    }

    public Certificacao getCertificacao() {
        return certificacao;
    }

    public void setCertificacao(Certificacao certificacao) {
        this.certificacao = certificacao;
    }

    public List<Certificacao> getModelCertificacoes() {
        if (this.modelCertificacoes == null) {
            atualizarModelCertificacoes();
        }
        return modelCertificacoes;
    }

    public void setModelCertificacoes(List<Certificacao> modelCertificacoes) {
        this.modelCertificacoes = modelCertificacoes;
    }
}
